<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class User extends Model
{
    const CREATED_AT = 'insert_time';
    const UPDATED_AT = 'update_time';
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'lexiang_user';

    protected $primaryKey = 'user_id';

    public $timestamps = false;

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['tel','nicename','pic','password','mobile_id','insert_time','update_time'];

    public function add($data)
    {
        $nowtime = time();
        $this_ = $this->where('small_program_code',$data['openid'])->first();
        if(!$this_){
            $this_ = new User;
            $this_->insert_time = $nowtime;
        }
        $this_->nicename = $data['nicename'];
        $this_->avatar = $data['avatar'];
        $this_->small_program_code = $data['openid'];
        $this_->small_program_session = $data['session_key'];
        if(isset($data['unionid'])){
            $this_->unionid = $data['unionid'];
        }
        $this_->update_time = $nowtime;
        $this_->login_time = $nowtime;
        if($data['share_userid']){
            $this_->share_userid = $data['share_userid'];
        }
        if($this_->save()){
            return [
                'success'=>true,
                'code'=>'000',
                'message'=>'保存成功',
                'data'=>[
                    'avatar'=>$this_->avatar,
                    'nicename'=>$this_->nicename,
                    'small_program_code'=>$this_->small_program_code,
                    'tel'=>$this_->tel,
                    'user_id'=>$this_->user_id,
                    'account'=>$this_->account,
                    'unionid'=>$this_->unionid,
                    'small_program_session'=>$data['session_key'],
                    'appid'=>env('CLIENT_WXAPPID'),
                ]
            ];
        }else{
            return [
                'success'=>false,
                'code'=>'003',
                'message'=>'网络出错，请稍后再试',
                'data'=>[]
            ];
        }
    }

    function getUsers($request,$usertype=false)
    {
        return User::where($usertype)
        ->where(function($q)use($request){
            $key = $request->input('s',false);
            $type = $request->input('t',false);
            if($key){
                if($type=='name'){
                    $q = $q->where('nicename','like','%'.$key.'%');
                }else if($type=='tel'){
                    $q = $q->where('tel','like','%'.$key.'%');
                }else if($type=='number'){
                    $q = $q->where('user_id',$key);
                }
            }
            if($request->input('circle_id',false)){
                $q = $q->where('circle_id',$request->input('circle_id',false));
            }
            if($request->input('circle_range_id',false)){
                $q = $q->where('circle_range_id',$request->input('circle_range_id',false));
            }
            return $q;
        })
        ->select(DB::raw('*,null as circle_name,null as sucircle_name,null as share_user_name'))
        ->orderBy('insert_time','desc')
        ->paginate(15,['s','t','circle_id','circle_range_id']);
    }

    public function getPicAttribute($v)
    {
        return $v ?? "http://jiashilexiang.oss-cn-beijing.aliyuncs.com/PictureOfHead/20190829/f46619fbf2b155f813e15a251d359632.jpg";
    }

    public function getNicenameAttribute($v)
    {
        return empty($v) ? $this->tel : $v;
    }

    public function getBuyCardYxAttribute($v)
    {
        $albuycard = MealUserCard::where([
            ['card_invalid','=','0'],
            ['users_id','=',$this->user_id]
        ])->count();
        if($albuycard <= 0){
            return '未购卡有意向';
        }else if($albuycard >= 1){
            return '存在购卡记录但未再次购买';
        }
    }

    /**
     * 商圈
     */
    public function getCircleNameAttribute($v)
    {
        return Circle::where('circle_id',$this->circle_id)->value('circle_name') ?? '未设置';
    }

    /**
     * 商圈
     */
    public function getSucircleNameAttribute($v)
    {
        return Circle::where('circle_id',$this->circle_range_id)->value('circle_name') ?? '未设置';
    }

    public function getShareUserNameAttribute($v){
        return User::where('user_id',$this->share_userid)->value('nicename') ?? '无';
    }

    /**
     * 余额
     */
    public function getAccountAttribute($v){
        return $v;
    }
}
